<?php

/**
 * This is the model class for table "sign_logs".
 *
 * The followings are the available columns in table 'sign_logs':
 * @property string $id
 * @property string $uid
 * @property string $sign_date
 * @property string $update_time
 */
class SignLogs extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return SignLogs the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'sign_logs';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('uid, sign_date', 'required'),
            array('uid, sign_date', 'length', 'max' => 11),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, uid, sign_date, update_time', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => '系统编号',
            'uid' => '会员编号',
            'sign_date' => '签到时间',
            'update_time' => '录入时间',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('uid', $this->uid, true);
        $criteria->compare('sign_date', $this->sign_date, true);
        $criteria->compare('update_time', $this->update_time, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    /**
     * 生成日历
     * @param string $date '2012-12'
     */
    public function SimCalendar($date) {
        //$weekArr = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
        //$uid = Yii::app()->user->id;
        
        echo '<table width="266" border="0" align="center" cellpadding="0" cellspacing="0" style=" margin:0 au
            
to;"><thead><tr>
            <th class=" Usfin2Cin3 Usfin2Cin4 " style="text-align:right;">Sun</th>
            <th class=" Usfin2Cin4">Mon</th>
            <th class="Usfin2Cin3 Usfin2Cin4">Tue</th>
            <th class="Usfin2Cin3 Usfin2Cin4">Wed</th>
            <th class="Usfin2Cin3 Usfin2Cin4">Thu</th>
            <th class="Usfin2Cin3 Usfin2Cin4">Fri</th>
            <th class="Usfin2Cin3 Usfin2Cin4">Sat</th>
            </tr></thead><tbody>';
        $date_array = explode('-', $date);
        $start_week = 0; //从星期天开始为0
        $month = cal_days_in_month(CAL_GREGORIAN, $date_array[1], $date_array[0]); //当月的天数
        $wstar = date('w', strtotime($date . '-01')); //当月从星期几天始
        $rows = ceil(($wstar + $month) / 7); //总行数
        $mday = 1; //第几天
        //获取用户签到数据
        $uid = Yii::app()->user->id;
        $s = strtotime($date.'-01');
        $e = strtotime($date.'-'.$month);
        $logs = CHtml::listData($this->findAll("uid='$uid' AND sign_date BETWEEN $s AND $e"), 'sign_date', 'sign_date');
        
        for ($i = 0; $i < $rows; $i++) {
            echo '<tr>';
            for ($d = 0; $d < 7; $d++) {
                $nowday = 7 * $i + $d + $start_week;
                if ($nowday >= $wstar && $mday <= $month) {
                    $time = strtotime($date . '-' . $mday);
                    $className = '';
                    if(isset($logs[$time]))
                    {
                        $className = 'check_in';
                    }
                    $temp = date('d', $time);
                    echo '<td id="check_day_'.$temp.'" class="Usfin2Cin2 Usfin2Cin5 '.$className.'">' . $temp . '</td>';
                    $mday++;
                } else {
                    echo '<td class="Usfin2Cin2 ">&nbsp;</td>';
                }
            }
            echo '</tr>';
        }

        echo '</tbody></table>';
    }

}